package com.kye.express.business.customer_code.bind_customer_code;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;

import com.jakewharton.rxbinding2.support.v7.widget.RxToolbar;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.customer_code.customer_code_manager.CustomerCodeManagerActivity;
import com.kye.express.databinding.ActivityBindCustomerCodeBinding;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.user.User;

import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

public class BindCustomerCodeActivity extends BaseAppCompatActivity implements BindCustomerCodeContract.BindCustomerCodeView {

    private ActivityBindCustomerCodeBinding mBinding;
    private BindCustomerCodeContract.BindCustomerCodePresenter mPresenter;
    private Dialog mDialog;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, BindCustomerCodeActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_bind_customer_code);

        initEvent();
        initData();
    }

    private void initEvent() {
        setSupportActionBar(mBinding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        RxToolbar.navigationClicks(mBinding.toolbar)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        finish();
                    }
                });


        Observable.combineLatest(RxTextView.textChanges(mBinding.editTextCustomerCode), RxTextView.textChanges(mBinding.editTextCustomerPassword), new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence charSequence, CharSequence charSequence2) throws Exception {
                return !TextUtils.isEmpty(charSequence) && !TextUtils.isEmpty(charSequence2);
            }
        })
                .compose(this.<Boolean>bindToLifecycleDestroy())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        mBinding.buttonConfirm.setEnabled(aBoolean);
                    }
                });

        RxView.clicks(mBinding.buttonConfirm)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mPresenter.bindCustomerCode(mBinding.getUser().getPhone(), mBinding.editTextCustomerCode.getText().toString().trim(), mBinding.editTextCustomerPassword.getText().toString().trim());
                    }
                });
    }

    private void initData() {
        mPresenter = new BindCustomerCodeContract.BindCustomerCodePresenter();
        mPresenter.bind(this);
        mPresenter.getCompanyName();
    }

    @Override
    public void onUser(User user) {
        mBinding.setUser(user);
    }

    @Override
    public void onSuccess() {
        CustomerCodeManagerActivity.startActivity(this);
        finish();
    }

    @Override
    public void onCompanyName(String s) {
        mBinding.setCompanyName(s);
    }

    @Override
    public void onLoading() {
        if (mDialog == null) {
            mDialog = new KyeProgressDialog.Builder(this).build();
        }
        mDialog.show();
    }

    @Override
    public void onLoaded() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }
}
